package com.jd.fruit.model.admin.auth;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jd.fruit.common.annotation.Table;
import com.jd.fruit.common.model.BaseModel;
import com.jd.fruit.common.util.JsonConvertUtil;
import com.jd.fruit.common.util.StringUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 角色
 * 
 */
@SuppressWarnings("serial")
@Table("t_menu")
public class Menu extends BaseModel<Menu> { 
	public static final Menu dao = new Menu();
	
	
	/**
	 * 保存菜单
	 * @param role
	 */
	public void save(Menu menu){
		try {
			menu.set("id",StringUtil.getKey());
			menu.save();
		} catch (Exception e) {
			throw new RuntimeException("菜单保存异常！", e);
		}
	}

	/**
	 * 读取菜单tree
	 * @param type
	 * @return
	 */
	public JSONArray loadTree(String rolemap){
		String sql="SELECT * FROM `t_menu` order by priority";
		List<Record> recordList=Db.find(sql);
		//预留角色map，在以后所属栏目默认选中
	 
	    if(recordList.size()>0){
	    	 return JsonConvertUtil.tranformToJSON(recordList, rolemap);
		 }
	    //默认数据
	    JSONArray defaultArray=new JSONArray();
		JSONObject defaultResult=new JSONObject();
		defaultResult.put("data", "no have data!");
		defaultArray.add(defaultResult);
		return defaultArray;
	} 
	
	/**
	 * 读取菜单tree
	 * @param loginUserId
	 * @return
	 */
	public  String loadMenu(String loginUserId){ 
		String sql="SELECT m.* FROM t_admin_role ar " +
				"RIGHT JOIN t_role r ON ar.`role_id`=r.`id` " +
				"RIGHT JOIN t_role_menu  rm ON rm.`role_id`=r.`id`" +
				"RIGHT JOIN t_menu  m ON rm.`menu_id`=m.`id` " +
				"WHERE ar.`admin_id`=? order by priority";
		List<Record> recordList=Db.find(sql,loginUserId);
		//预留角色map，在以后所属栏目默认选中
		//预留角色map，在以后所属栏目默认选中
		if(recordList.size()>0){
	       return JsonConvertUtil.getFormatData(recordList);
		}
		 //默认数据
		JSONObject defaultResult=new JSONObject();
		defaultResult.put("data", "no have data!");
		return defaultResult.toJSONString();
	} 
	
	
	/**
	 * 根据adminid获得URL验证
	 * @param roleid
	 */
	public static boolean checkRole(String adminid,String uri) {
		uri=uri.replace("/jd_drive", "");
		uri=uri.substring(0, uri.lastIndexOf("/")); 
		String sql="SELECT m.* FROM t_admin_role ar " +
		"RIGHT JOIN t_role r ON ar.`role_id`=r.`id` " +
		"RIGHT JOIN t_role_menu  rm ON rm.`role_id`=r.`id`" +
		"RIGHT JOIN t_menu  m ON rm.`menu_id`=m.`id` " +
		"WHERE ar.`admin_id`=? AND m.`url` LIKE ?  order by priority";
		List<Record> result=Db.find(sql,adminid,"%"+uri+"%");
		return result.size()>0?true:false;
	}
	
	
	/**
	 * 读取菜单tree
	 * @param type 
	 * @return 
	 */
	public String loadTreeGrid(){
		String sql="SELECT * FROM `t_menu` order by priority";
		List<Record> recordList=Db.find(sql);
		//预留角色map，在以后所属栏目默认选中
		if(recordList.size()>0){
	       return JsonConvertUtil.getFormatData(recordList);
		}
		 //默认数据
		JSONObject defaultResult=new JSONObject();
		defaultResult.put("data", "no have data!");
		return defaultResult.toJSONString();
	} 
	
	@Override
	public void initWhere() {
		// TODO Auto-generated method stub
		
	}
	
	
}
